import { ref } from "vue";
import { storeToRefs } from "pinia";
import { useRouter } from "vue-router";
import { useUserStore } from "@/stores/userStore";
import { el } from "element-plus/es/locales.mjs";
import { ElMessage } from "element-plus";
import axios from "axios";
import { identity } from "lodash";
// import { log } from "echarts/types/src/util/log.js";
/**
 * 使用hooks技术包装登录功能，需要将该功能包装在一个方法中，
 * 并暴露出去（默认暴露），分别暴露需要方法名，
 * 并将需要的数据和方法返回出去
 * @returns
 */
export default function () {
    const router = useRouter();
    const login_Name = ref("");
    const login_passWord = ref("");
    const userStore = useUserStore();
    const { usertype, useridentityfer, username_store, userid } =
        storeToRefs(userStore);
    const User = ref({ loginName: login_Name, loginPassword: login_passWord });

    // 将 login 函数改为 async
    async function login() {
        try {
            const response = await axios.post("/api/login/main", User.value);
            if (response.data.code === 200) {
                // 取出后端返回的 data
                const { role, indetifier, username, id } = response.data.data;
                usertype.value = role;
                useridentityfer.value = indetifier;
                username_store.value = username;
                userid.value = id;
                console.log("登录用户:",response.data.data)
            } else {
                ElMessage.error("错误：" + response.data.message);
            }
        } catch (error: any) {
            ElMessage.error(
                "请求失败：" +
                (error.response?.data?.message || "服务器错误")
            );
        }
    }

    // 在 handleLogin 中等待 login 完成后再进行跳转
    async function handleLogin() {
        // 调用 login，等待其完成
        await login();
        switch (usertype.value) {
            case "1": {
                ElMessage.success(
                    "登录成功" +
                    "  用户名：" +
                    username_store.value +
                    "用户类型：" +
                    usertype.value
                );
                router.push({
                    name: "studentHome",
                });
                break;
            }
            case "2": {
                ElMessage.success(
                    "登录成功" +
                    "  用户名：" +
                    username_store.value +
                    "用户类型：" +
                    usertype.value
                );
                router.push({
                    name: "laborHome",
                });
                break;
            }
            case "3": {
                ElMessage.success(
                    "登录成功" +
                    "  用户名：" +
                    username_store.value +
                    "用户类型：" +
                    usertype.value
                );
                router.push({
                    name: "instituteHome",
                });
                break;
            }
            case "4": {
                ElMessage.success(
                    "登录成功" +
                    "  用户名：" +
                    username_store.value +
                    "用户类型：" +
                    usertype.value
                );
                router.push({
                    name: "divisionHome",
                });
                break;
            }
            default: {
                ElMessage.error(
                    "登录失败" +
                    "  用户名：" +
                    username_store.value +
                    "密码：" +
                    login_passWord.value +
                    "用户类型：" +
                    usertype.value
                );
            }
        }
    }

    function loginOut() {
        usertype.value = "0";
        useridentityfer.value = 0;
        username_store.value = "";
        userid.value = 0;
    }

    function register() {
        router.push({
            name: "register",
        });
    }

    return {
        login,
        handleLogin,
        loginOut,
        register,
        login_Name,
        login_passWord,
        userStore
    };
}
